package com.agritech.controller;

import com.agritech.common.Result;
import com.agritech.entity.Answer;
import com.agritech.service.AnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 回答控制器
 */
@RestController
@CrossOrigin
@RequestMapping("/api/answers")
public class AnswerController {

    @Autowired
    private AnswerService answerService;

    /**
     * 根据问题ID获取回答列表
     */
    @GetMapping("/question/{questionId}")
    public Result getAnswersByQuestionId(@PathVariable Long questionId) {
        return Result.ok(answerService.getAnswersByQuestionId(questionId));
    }

    /**
     * 根据ID获取回答详情
     */
    @GetMapping("/{id}")
    public Result getAnswerById(@PathVariable Long id) {
        return Result.ok(answerService.getAnswerById(id));
    }

    /**
     * 添加新回答
     */
    @PostMapping
    public Result addAnswer(@RequestBody Answer answer) {
        boolean success = answerService.addAnswer(answer);
        return success ? Result.ok("添加成功") : Result.error("添加失败");
    }

    /**
     * 更新回答信息
     */
    @PutMapping
    public Result updateAnswer(@RequestBody Answer answer) {
        boolean success = answerService.updateAnswer(answer);
        return success ? Result.ok("更新成功") : Result.error("更新失败");
    }
}    